﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OpenSoftStore.Client.UIL.UserControls
{
    class YhSoftRadioButton : RadioButton
    {
        public YhSoftRadioButton()
        {
            this.KeyUp += new KeyEventHandler(KeyUps);
            this.Enter += new EventHandler(Enters);
            this.Leave += new EventHandler(Leaves);
        }

        private void Enters(object sender, EventArgs e)
        {
            this.BackColor = Color.FromArgb(179, 221, 150);
        }

        private void Leaves(object sender, EventArgs e)
        {
            this.BackColor = System.Drawing.Color.Transparent;
        }

        private static void GetControl(Control control, List<YhSoftRadioButton> controls)
        {
            for (int i = 0; i < control.Controls.Count; i++)
            {
                var tempcontrol = control.Controls[i];
                if (tempcontrol.GetType().Name != "YhSoftRadioButton") continue;
                controls.Add(tempcontrol as YhSoftRadioButton);
            }
        }

        private void KeyUps(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                var findForm = FindForm();
                if (findForm == null) return;
                findForm.TabIndex = TabIndex + 1;
                findForm.SelectNextControl(this, true, true, true, true);
            }
            if (e.KeyCode == Keys.Add)
            {
                var controls = new List<YhSoftRadioButton>();
                GetControl(this.Parent, controls);
                controls=controls.OrderBy(m => m.TabIndex).ToList();
                var index=controls.FindIndex(m => m.Name == this.Name);
                if (index + 1 >= controls.Count) index = 0;
                if (controls[index].Checked)
                    controls[index + 1].Select();
                else
                    controls[index].Select();
            }
        }
    }
}
